package com.mxpio.erp.common.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.jpa.entity.BizNoticeType;
import com.mxpio.erp.common.service.BizNoticeTypeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "BizNoticeTypeController", description = "通知单类别接口")
@RestController
@RequestMapping("/erp/common/notice")
public class BizNoticeTypeController {
	@Autowired
    private BizNoticeTypeService bizNoticeTypeService;

    @GetMapping("list")
    @Operation(summary = "通知单类别列表", description = "获取通知单类别列表（无分页）", method = "GET")
    public Result<List<BizNoticeType>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<BizNoticeType> bizNoticeTypes = bizNoticeTypeService.list(BizNoticeType.class, criteria);
        return Result.OK(bizNoticeTypes);
    }

    @GetMapping("page")
    @Operation(summary = "通知单类别列表", description = "获取通知单类别列表（分页）", method = "GET")
    public Result<Page<BizNoticeType>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BizNoticeType> bizNoticeTypes = bizNoticeTypeService.listPage(BizNoticeType.class, page, criteria);
        return Result.OK(bizNoticeTypes);
    }

    @GetMapping("list/{typeCode}")
    @Operation(summary = "根据typeCode获取通知单类别", description = "根据typeCode获取通知单类别", method = "GET")
    public Result<BizNoticeType> getById(@Parameter(description="类别编号") @PathVariable(name = "typeCode", required = true) String typeCode) {
        BizNoticeType bizNoticeType = bizNoticeTypeService.getById(BizNoticeType.class, typeCode);
        return Result.OK(bizNoticeType);
    }

    @PostMapping("add")
    @Operation(summary = "新增通知单类别", description = "新增通知单类别", method = "POST")
    public Result<BizNoticeType> add(@RequestBody BizNoticeType bizNoticeType) {
        bizNoticeTypeService.save(bizNoticeType);
        return Result.OK(bizNoticeType);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑通知单类别", description = "编辑通知单类别", method = "PUT")
    public Result<BizNoticeType> edit(@RequestBody BizNoticeType bizNoticeType) {
        bizNoticeTypeService.update(bizNoticeType);
        return Result.OK(bizNoticeType);
    }

    @DeleteMapping("remove/{typeCodes}")
    @Operation(summary = "根据bizNo删除通知单类别", description = "删除通知单类别", method = "DELETE")
    public Result<BizNoticeType> remove(@Parameter(description="类别编号，多个编号用英文逗号分隔") @PathVariable(name = "typeCodes", required = true) String typeCodes) {
        String[] typeCode = typeCodes.split(",");
        for(String key : typeCode){
            bizNoticeTypeService.delete(BizNoticeType.class, key);
        }
        return Result.OK();
    }
}
